package com.yu1996.gulimall.ware.service.impl;

import com.yu1996.common.utils.R;
import com.yu1996.gulimall.ware.feign.ProductFeignService;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu1996.common.utils.PageUtils;
import com.yu1996.common.utils.Query;

import com.yu1996.gulimall.ware.dao.WareSkuDao;
import com.yu1996.gulimall.ware.entity.WareSkuEntity;
import com.yu1996.gulimall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity>
    implements WareSkuService {

  @Autowired private ProductFeignService productFeignService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
    /** skuId: wareId: 1 */
    String skuId = (String) params.get("skuId");
    if (!StringUtils.isEmpty(skuId) && !"0".equalsIgnoreCase(skuId)) {
      wrapper.eq("sku_id", skuId);
    }
    String wareId = (String) params.get("wareId");
    if (!StringUtils.isEmpty(wareId) && !"0".equalsIgnoreCase(wareId)) {
      wrapper.eq("ware_id", wareId);
    }
    IPage<WareSkuEntity> page = this.page(new Query<WareSkuEntity>().getPage(params), wrapper);

    return new PageUtils(page);
  }
  /**
   * 增加商品库存
   *
   * @param skuId 菜单项id
   * @param wareId 仓库id
   * @param skuNum 商品采购量
   */
  @Override
  public void addStock(Long skuId, Long wareId, Integer skuNum) {
    // 判断之前该仓库有无该商品
    List<WareSkuEntity> wareSkuEntities =
        this.baseMapper.selectList(
            new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
    if (wareSkuEntities == null || wareSkuEntities.size() == 0) {
      // 无该商品需要创建保存
      WareSkuEntity entity = new WareSkuEntity();
      entity.setSkuId(skuId);
      entity.setWareId(wareId);
      entity.setStock(skuNum);
      entity.setStockLocked(0);
      // 远程调用查找sku的名称
      try {
        R info = productFeignService.info(skuId);
        if (info.getCode() == 0) {
          Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
          entity.setSkuName((String) data.get("skuName"));
        }
      } catch (Exception e) {

      }
      this.baseMapper.insert(entity);
    } else {
      this.baseMapper.addStock(skuId, wareId, skuNum);
    }
  }
  /**
   * 根据skuid批量查找库存数
   *
   * @param skuIds
   * @return
   */
  @Transactional
  @Override
  public Map<Long, Long> getSkusHasStock(List<Long> skuIds) {
    Map<Long, Long> stocks = new HashMap<>();
    for (Long skuId : skuIds) {
      Long stock = this.baseMapper.getSkusHasStock(skuId);
      if (stock != null) stocks.put(skuId, stock);
    }
    return stocks;
  }
}
